package com.pozu.new_retail.controllers;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.dao.model.Goods;
import com.pozu.new_retail.dao.model.GoodsExt;
import com.pozu.new_retail.dao.model.ShoppingCart;
import com.pozu.new_retail.service.GoodsExtService;
import com.pozu.new_retail.service.GoodsService;
import com.pozu.new_retail.service.ShoppingCartService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.Tools;

@Controller
public class ShoppingCartController {
	
	@Autowired
	private ShoppingCartService shoppingCartService;
	@Autowired
	private GoodsExtService goodsExtService;
	@Autowired
	private GoodsService goodsService;

	@ResponseBody
	@RequestMapping(value = "/api/shoppingCarts", method = RequestMethod.GET)
	public Object get(HttpServletRequest request) throws Exception{

		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		int userId = Integer.parseInt(request.getHeader("userId"));

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("userId", userId);
		map.put("state", 0);
		map.put("appId", appId);

		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = shoppingCartService.queryByMap(map);

		return jr;
	}
	/****
	 * 加入购物车
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/api/shoppingCarts", method = RequestMethod.POST)
	public Object add(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		ShoppingCart requestShoppingCart = Tools.getRequestObject(request, ShoppingCart.class);

		long userId = Long.parseLong(request.getHeader("userId"));
		
		int goodsExtId = requestShoppingCart.getGoodsExtId();	//商品扩展属性
		
		int number = requestShoppingCart.getNumber();//购买数量
		
		JsonResult jr = new JsonResult();	
		
		HashMap<String, Object> map = new HashMap<String,Object>();
		
		map.put("userId", userId);
		map.put("goodsExtId", goodsExtId);
		
		GoodsExt ext = goodsExtService.queryByGoodsExtId(goodsExtId);
		if(null == ext || ext.getState()!=0 ||ext.getPrice()<=0){
			jr.respCode = CodeConstants.GOODSNOTEXISTED_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.GOODSNOTEXISTED_MSG);
			return jr;
		}
		
		if(ext.getNumber() < number){
			jr.respCode = CodeConstants.GOODSNOTENOUGH_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.GOODSNOTENOUGH_MSG);
			return jr;
		}
		
		Goods goods = goodsService.queryInfoByGoodsId(ext.getGoodsId());
		
		if(null == goods || goods.getState()!=0 ||goods.getGoodsPrice()<=0){
			jr.respCode = CodeConstants.GOODSNOTEXISTED_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.GOODSNOTEXISTED_MSG);
			return jr;
		}
		
		if (goods.getSaleType() == 0) {
			//预售商品不能加入购物车
			jr.respCode = CodeConstants.PRESALEADDERROR_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.PRESALEADDERROR_MSG);
			return jr;
		}
		
		ShoppingCart cart = shoppingCartService.queryByGoodsExtIdAndUserId(map);
		
		if (null != cart && cart.getType() != 2) {
			//非砍价的可合并
			ShoppingCart temp = new ShoppingCart();
			temp.setShoppingCartId(cart.getShoppingCartId());
			temp.setNumber(requestShoppingCart.getNumber()+cart.getNumber());
			shoppingCartService.updateShoppingCart(temp);
			
			jr.respCode = CodeConstants.SUCCESS_CODE;
			jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);		
			
			return jr;
		}

		if (goods.getGroupFlag() == 1 && goods.getSaleType() == 1) {
			requestShoppingCart.setType(1);
		}else {
			requestShoppingCart.setType(0);
		}
		
		requestShoppingCart.setAppId(appId);
		requestShoppingCart.setUserId(userId);
		
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);		
		
		shoppingCartService.saveShoppingCart(requestShoppingCart);
		
		return jr;
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/shoppingCarts", method = RequestMethod.PUT)
	public Object update(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序

		int userId = Integer.parseInt(request.getHeader("userId"));
		
		JsonResult jr = new JsonResult();
		
		ShoppingCart requestShoppingCart = Tools.getRequestObject(request, ShoppingCart.class);
		
		long shoppingCartId = requestShoppingCart.getShoppingCartId();//主键id

		ShoppingCart cart = shoppingCartService.queryByShoppingCartId(shoppingCartId);
		
		if(null == cart){
			jr.respCode = CodeConstants.CARTGOODSNOTEXISTED_MSG;
			jr.respMsg = CodeProperties.getString(CodeConstants.CARTGOODSNOTEXISTED_MSG);
			return jr;
		}

		if(requestShoppingCart.getNumber() != null){

			if(cart.getType() == 2){
				jr.respCode = CodeConstants.SHAREDGOODSLIMITED_MSG;
				jr.respMsg = CodeProperties.getString(CodeConstants.SHAREDGOODSLIMITED_MSG);
				return jr;
			}
			
			int number = requestShoppingCart.getNumber();//购买数量
			
			GoodsExt ext = goodsExtService.queryByGoodsExtId(cart.getGoodsExtId());
			if(null == ext || ext.getState()!=0 || ext.getPrice()<=0 ){
				//设置购物车中的商品失效
				
				ShoppingCart temp = new ShoppingCart();
				temp.setShoppingCartId(shoppingCartId);
				temp.setState(-1);
				shoppingCartService.updateShoppingCart(temp);
				
				jr.respCode = CodeConstants.GOODSNOTEXISTED_MSG;
				jr.respMsg = CodeProperties.getString(CodeConstants.GOODSNOTEXISTED_MSG);
				return jr;
			}
			
			if(ext.getNumber()<number){
				jr.respCode = CodeConstants.GOODSNOTENOUGH_MSG;
				jr.respMsg = CodeProperties.getString(CodeConstants.GOODSNOTENOUGH_MSG);
				return jr;
			}
			cart.setNumber(number);
		}

		if(requestShoppingCart.getState() != null){
			int state = requestShoppingCart.getState();//状态2删除
			cart.setState(state);
		}

		shoppingCartService.updateShoppingCart(cart);

		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("userId", userId);
		map.put("state", 0);
		map.put("appId", appId);

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = shoppingCartService.queryByMap(map);
		
		return jr;
	}
	
}
